package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.*;
import com.wzcl.app.dao.repository.AppUserRepository;
import com.wzcl.app.dao.repository.TestPaperDownloadRepository;
import com.wzcl.app.dao.repository.TestPaperRepository;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.dto.TestPaperDownloadDto;
import com.wzcl.app.model.dto.WorkShowDto;
import com.wzcl.app.service.TestPaperDownloadService;
import com.wzcl.app.utils.DateUtil;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author beingven
 * @Description TODO
 * @createTime 2021年01月10日 13:42:00
 */
@Service
public class TestPaperDownloadServiceImpl implements TestPaperDownloadService {
    @Resource
    private AppUserRepository appUserRepository;
    @Resource
    private TestPaperRepository testPaperRepository;
    @Resource
    private TestPaperDownloadRepository testPaperDownloadRepository;

    @Override
    public TestPaperDownload createDownloadRecord(TestPaperDownloadDto testPaperDownloadDto) {
        String phone = SecurityContextHolder.getContext().getAuthentication().getName();
        AppUser appUser = appUserRepository.findByPhone(phone);
        TestPaper testPaper = testPaperRepository.findPaperById(testPaperDownloadDto.getTestPaperId());
        TestPaperDownload testPaperDownload = testPaperDownloadRepository.save(new TestPaperDownload(appUser, testPaper, DateUtil.getDateBySystemTime(System.currentTimeMillis())));
        return testPaperDownload;
    }

    @Override
    public MyPage retrieveByCondition(TestPaperDownloadDto testPaperDownloadDto) {
        return new MyPage(testPaperDownloadRepository.findAll(getSpecification(testPaperDownloadDto), testPaperDownloadDto.getPageable()));
    }

    //条件筛选
    public Specification<TestPaperDownload> getSpecification(TestPaperDownloadDto testPaperDownloadDto){
        return (Specification<TestPaperDownload>) (root, criteriaQuery, criteriaBuilder) -> {
            ArrayList<Predicate> list = new ArrayList<>();
            //条件过滤
            if (testPaperDownloadDto.getUserName() != null && testPaperDownloadDto.getUserName() != ""){
                //创建到指定属性的内部连接
                Join<TestPaperDownload, AppUser> appUser = root.join("appUser");
                list.add(criteriaBuilder.like(appUser.get("realName"), "%"+testPaperDownloadDto.getUserName()+"%"));
            }
            if (testPaperDownloadDto.getGradeName() != null && testPaperDownloadDto.getGradeName() != ""){
                Join<TestPaperDownload, TestPaper> testPaper = root.join("testPaper");
                list.add(criteriaBuilder.like(testPaper.get("gradeName"), "%"+testPaperDownloadDto.getGradeName()+"%"));
            }
            if (testPaperDownloadDto.getPhone() != null && testPaperDownloadDto.getPhone() != ""){
                Join<TestPaperDownload, AppUser> appUser = root.join("appUser");
                list.add(criteriaBuilder.like(appUser.get("phone"), "%"+testPaperDownloadDto.getPhone()+"%"));
            }
            if (testPaperDownloadDto.getPaperName() != null && testPaperDownloadDto.getPaperName() != ""){
                Join<TestPaperDownload, TestPaper> testPaper = root.join("testPaper");
                list.add(criteriaBuilder.like(testPaper.get("name"), "%"+testPaperDownloadDto.getPaperName()+"%"));
            }
            // 排序
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("downloadTime")));
            //将两个查询条件联合起来之后返回Predicate对象
            return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
        };
    }
}
